package com.softlab.retailwholesalesystem.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softlab.retailwholesalesystem.entity.GoodsEntity;
import com.softlab.retailwholesalesystem.entity.StoreEntity;
import com.softlab.retailwholesalesystem.mapper.GoodsMapper;
import com.softlab.retailwholesalesystem.mapper.StoreMapper;
import com.softlab.retailwholesalesystem.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, GoodsEntity> implements IGoodsService {

    @Autowired
    private GoodsMapper mapper;

    @Autowired
    private StoreMapper storeMapper;


    @Override
    public List<GoodsEntity> searchGoods(GoodsEntity goodsEntity){
        QueryWrapper<GoodsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (goodsEntity.getGoodsId() != null) wrapper.eq("goods_id",goodsEntity.getGoodsId());
        if (goodsEntity.getGoodsName() != null) wrapper.eq("goods_name",goodsEntity.getGoodsName());
        if (goodsEntity.getStockPrice() != null) wrapper.eq("stock_price",goodsEntity.getStockPrice());
        if (goodsEntity.getaPrice() != null) wrapper.eq("a_price",goodsEntity.getaPrice());
        if (goodsEntity.getbPrice() != null) wrapper.eq("b_price",goodsEntity.getbPrice());
        if (goodsEntity.getcPrice() != null) wrapper.eq("c_price",goodsEntity.getcPrice());

        List<GoodsEntity> goodsList = mapper.selectList(wrapper);

        for (GoodsEntity goods : goodsList){
            QueryWrapper<StoreEntity> qw = new QueryWrapper<>();
            qw.eq("is_delete", 0);
            qw.eq("goods_id", goods.getGoodsId());

            List<StoreEntity> storeList = storeMapper.selectList(qw);
            Double unSold = 0.0;
            for (StoreEntity store : storeList){
                unSold += store.getGoodsNumber() * goods.getStockPrice();
            }
            goods.setUnsoldMoney(unSold);
        }

        return goodsList;
    }

    @Override
    public List<GoodsEntity> getAll() {
        QueryWrapper<GoodsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete", 0);
        List<GoodsEntity> goodsList = mapper.selectList(wrapper);

        for (GoodsEntity goods : goodsList){
            QueryWrapper<StoreEntity> qw = new QueryWrapper<>();
            qw.eq("is_delete", 0);
            qw.eq("goods_id", goods.getGoodsId());

            List<StoreEntity> storeList = storeMapper.selectList(qw);
            Double unSold = 0.0;
            for (StoreEntity store : storeList){
                unSold += store.getGoodsNumber() * goods.getStockPrice();
            }
            goods.setUnsoldMoney(unSold);
        }

        return goodsList;
    }

}
